/////////////////////////////////////////////////////////////////////////////
// Name:        systemsdialog.cpp
// Purpose:     
// Author:      Erwan Le Martelot
// Modified by: 
// Created:     20/04/2007 11:23:00
// RCS-ID:      
// Copyright:   
// Licence:     
/////////////////////////////////////////////////////////////////////////////

#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "systemsdialog.hpp"
#endif

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
////@end includes

#include "systemsdialog.hpp"

// ----- My Includes ----- //
#include <exception>
// ----------------------- //

// ---- My Namespaces ---- //
using namespace std;
using namespace SC;
// ----------------------- //

////@begin XPM images
#include "SCANN_icon.xpm"
////@end XPM images

/*!
 * SystemsDialog type definition
 */

IMPLEMENT_DYNAMIC_CLASS( SystemsDialog, wxFrame )

/*!
 * SystemsDialog event table definition
 */

BEGIN_EVENT_TABLE( SystemsDialog, wxFrame )

////@begin SystemsDialog event table entries
	EVT_CLOSE( SystemsDialog::OnCloseWindow )

	EVT_LIST_ITEM_ACTIVATED( ID_LISTVIEW, SystemsDialog::OnListviewItemActivated )

	EVT_BUTTON( ID_HIDE_SYSTEMS_BUTTON, SystemsDialog::OnHideSystemsButtonClick )

////@end SystemsDialog event table entries

END_EVENT_TABLE()

/*!
 * SystemsDialog constructors
 */

SystemsDialog::SystemsDialog()
{
	Init();
}

SystemsDialog::SystemsDialog( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
	Init();
	Create(parent, id, caption, pos, size, style);
}

/*!
 * SystemsDialog creator
 */

bool SystemsDialog::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin SystemsDialog creation
	SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
	wxFrame::Create( parent, id, caption, pos, size, style );

	CreateControls();
	SetIcon(GetIconResource(wxT("src/SCANN_icon.xpm")));
	Centre();
////@end SystemsDialog creation
	return true;
}

/*!
 * SystemsDialog destructor
 */

SystemsDialog::~SystemsDialog()
{
////@begin SystemsDialog destruction
////@end SystemsDialog destruction
}

/*!
 * Member initialisation 
 */

void SystemsDialog::Init()
{
////@begin SystemsDialog member initialisation
	systemsList = NULL;
////@end SystemsDialog member initialisation
}

/*!
 * Control creation for SystemsDialog
 */

void SystemsDialog::CreateControls()
{    
////@begin SystemsDialog content construction
	SystemsDialog* itemFrame1 = this;

	wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
	itemFrame1->SetSizer(itemBoxSizer2);

	systemsList = new wxListView( itemFrame1, ID_LISTVIEW, wxDefaultPosition, wxDefaultSize, wxLC_REPORT|wxSUNKEN_BORDER );
	itemBoxSizer2->Add(systemsList, 1, wxGROW|wxALL, 0);

	wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
	itemBoxSizer2->Add(itemBoxSizer4, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 0);

	wxButton* itemButton5 = new wxButton( itemFrame1, ID_HIDE_SYSTEMS_BUTTON, _("Hide"), wxDefaultPosition, wxSize(150, -1), 0 );
	itemBoxSizer4->Add(itemButton5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

////@end SystemsDialog content construction
}

/*!
 * wxEVT_COMMAND_LIST_ITEM_ACTIVATED event handler for ID_LISTVIEW
 */

void SystemsDialog::OnListviewItemActivated( wxListEvent& event )
{
////@begin wxEVT_COMMAND_LIST_ITEM_ACTIVATED event handler for ID_LISTVIEW in SystemsDialog.
	// Before editing this code, remove the block markers.
	event.Skip();
////@end wxEVT_COMMAND_LIST_ITEM_ACTIVATED event handler for ID_LISTVIEW in SystemsDialog. 
}

/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_HIDE_SYSTEMS_BUTTON
 */

void SystemsDialog::OnHideSystemsButtonClick( wxCommandEvent& event )
{
	this->Hide();
////@begin wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_HIDE_SYSTEMS_BUTTON in SystemsDialog.
	// Before editing this code, remove the block markers.
	event.Skip();
////@end wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_HIDE_SYSTEMS_BUTTON in SystemsDialog. 
}

/*!
 * Should we show tooltips?
 */

bool SystemsDialog::ShowToolTips()
{
	return true;
}

/*!
 * Get bitmap resources
 */

wxBitmap SystemsDialog::GetBitmapResource( const wxString& name )
{
	// Bitmap retrieval
////@begin SystemsDialog bitmap retrieval
	wxUnusedVar(name);
	return wxNullBitmap;
////@end SystemsDialog bitmap retrieval
}

/*!
 * Get icon resources
 */

wxIcon SystemsDialog::GetIconResource( const wxString& name )
{
	// Icon retrieval
////@begin SystemsDialog icon retrieval
	wxUnusedVar(name);
	if (name == _T("src/SCANN_icon.xpm"))
	{
		wxIcon icon( SCANN_icon_xpm);
		return icon;
	}
	return wxNullIcon;
////@end SystemsDialog icon retrieval
}

void SystemsDialog::SetData(const SCProgram & program, const vector<SCSystem> & systems) {
	this->initSystemsList(program);
	this->UpdateAllData(systems);
}

void SystemsDialog::Update(const unsigned int idx, const SCSystem & sys) {
	wxListItem item;
	item.SetId(idx);
	this->systemsList->GetItem(item);
	
	item.SetColumn(2);
	item.SetText(this->cutWord(sys.GetSchema1()).c_str());
	this->systemsList->SetItem(item);

	item.SetColumn(3);
	item.SetText(this->cutWord(sys.GetKernel()).c_str());
	this->systemsList->SetItem(item);

	item.SetColumn(4);
	item.SetText(this->cutWord(sys.GetSchema2()).c_str());
	this->systemsList->SetItem(item);
}

void SystemsDialog::initSystemsList(const SCProgram & program) {
	try {
		this->systemsList->ClearAll();
		this->systemsList->InsertColumn(0,"Type");
		this->systemsList->SetColumnWidth(0,100);
		this->systemsList->InsertColumn(1,"Name");
		this->systemsList->SetColumnWidth(1,100);
		this->systemsList->InsertColumn(2,"Schemata1");
		this->systemsList->SetColumnWidth(2,200);
		this->systemsList->InsertColumn(3,"Kernel");
		this->systemsList->SetColumnWidth(3,200);
		this->systemsList->InsertColumn(4,"Schemata2");
		this->systemsList->SetColumnWidth(4,200);
		for (unsigned int i=0; i<program.GetSysInst().size(); ++i) {
			wxListItem item;
			item.SetId(i);
			item.SetColumn(0);
			item.SetText(program.GetSysDecl()[program.GetSysInst()[i].GetFirst()].GetFirst().c_str());
			this->systemsList->InsertItem(item);
			item.SetColumn(1);
			item.SetText(program.GetSysInst()[i].GetSecond().c_str());
			this->systemsList->SetItem(item);
		}
	}
	catch (exception & e) {
		wxMessageBox(_(e.what()));
	}
}

void SystemsDialog::UpdateAllData(const vector<SCSystem> & systems) {
	try {
		for (unsigned int i=0; i<systems.size(); ++i) {
			wxListItem item;
			item.SetId(i);
			this->systemsList->GetItem(item);
			const SCSystem & sys = systems[i];
			
			item.SetColumn(2);
			item.SetText(this->cutWord(sys.GetSchema1()).c_str());
			this->systemsList->SetItem(item);

			item.SetColumn(3);
			item.SetText(this->cutWord(sys.GetKernel()).c_str());
			this->systemsList->SetItem(item);

			item.SetColumn(4);
			item.SetText(this->cutWord(sys.GetSchema2()).c_str());
			this->systemsList->SetItem(item);
		}
	}
	catch (exception & e) {
		wxMessageBox(_(e.what()));
	}
}

/*!
 * wxEVT_CLOSE_WINDOW event handler for ID_SYSTEMS_DIALOG
 */

void SystemsDialog::OnCloseWindow( wxCloseEvent& event )
{
	this->Hide();
	event.Skip(false);
}

#define CUT_SIZE 10
string SystemsDialog::cutWord(const string & word) const {
	string result;
	unsigned int i=0;
	while (i<word.size()-CUT_SIZE) {
		if (!result.empty()) result += " ";
		result += word.substr(i,CUT_SIZE);
		i += CUT_SIZE;
	}
	if (i<word.size()) result += " " + word.substr(i,word.size()-i);
	return result;
}
